/**
 * 
 */
package com.tomkid.naojin.acts;

import java.lang.reflect.Method;

import com.tomkid.naojin.utils.CConstance;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * 
 * 
 * @author zhao.tb
 */
public class MainTabsActivity extends TabActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("MainTab", "Create");
		setContentView(R.layout.main);
		setTabs();
		
	}

	private void setTabs() {
		addTab(R.string.tab_Home, R.drawable.tab_home, ListMainActivity.class);
		addTab(R.string.tab_Favor, R.drawable.tab_favor, ListFavorActivity.class);
		addTab(R.string.tab_MoreApp, R.drawable.tab_moreapp, MoreAppActivity.class);

	}

	private void addTab(int labelId, int drawableId, Class<?> c) {
		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);
		
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}
	

	/* (Override)
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		Log.d("MainTab", "Destroy");
		super.onDestroy();
	}

	/**
	 * startActivity
	 * 
	 * @param i
	 * @param toLeft
	 *            True or False to right.
	 * @Description TODO
	 */
	public void startActivity(Intent i, boolean toLeft) {
		super.startActivity(i);
		startPendingAnimation(toLeft);
	}
	
	/**
	 * startPendingAnimation
	 * @param toLeft TODO
	 * @Description
	 *   TODO
	 */
	public void startPendingAnimation(boolean toLeft){
		Method method = null;
		try {
			if ((method = this.getClass().getMethod("overridePendingTransition", int.class, int.class)) != null) {
				if (toLeft) {
					method.invoke(this, R.anim.slide_enter, R.anim.slide_out);
				} else {
					method.invoke(this, R.anim.slide_outt, R.anim.slide_enterr);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
